package api

import (
	"api-gateway/basic/global"
	__ "api-gateway/basic/proto/good"
	"api-gateway/handler/req"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GoodCreate(c *gin.Context) {
	var req req.GoodCreate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数接收失败",
		})
		return
	}
	create, err := global.GoodClient.GoodCreate(c, &__.GoodCreateReq{
		GoodName:    req.GoodName,
		GoodClass:   int64(req.GoodClass),
		Price:       uint64(req.Price),
		Stock:       int64(req.Stock),
		Description: req.Description,
		ImageURL:    req.ImageURL,
		Status:      int64(req.Status),
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "判断错误" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": create,
	})
}

func GoodUpdate(c *gin.Context) {
	var req req.GoodUpdate
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "参数接收失败",
		})
		return
	}
	create, err := global.GoodClient.GoodUpdate(c, &__.GoodUpdateReq{
		Id:        req.Id,
		GoodName:  req.GoodName,
		GoodClass: int64(req.GoodClass),

		Price:       uint64(req.Price),
		Stock:       int64(req.Stock),
		Description: req.Description,
		ImageURL:    req.ImageURL,
		Status:      int64(req.Status),
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": 500,
			"msg":  "判断错误" + err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": create,
	})
}
